package net.optaplanner.machinetask;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.optaplanner.core.api.solver.Solver;
import org.optaplanner.core.api.solver.SolverFactory;


/**
 * 主控制程序
 */
public class App {

    public static void main(String[] args) {
        try {


            long  l1= System.currentTimeMillis();
            startPlan();
            long  l2= System.currentTimeMillis();

            System.out.println("");
            System.out.println("Total speed="+(l2-l1)/1000+" second");
        }
        catch(Exception e)
        {
            e.printStackTrace();

        }
    }

    private static void startPlan() throws   Exception{
        List<Machine> machines = getMachines();
        List<Task> tasks = getTasks();

        File f= new File("D:\\github_root\\optaplanner-study\\OptaPlannerMachineTask\\taskConfig.xml");
        System.out.println(f.getAbsolutePath());
        InputStream ins = new FileInputStream(f);

        //InputStream ins = App.class.getResourceAsStream("taskConfig.xml");
        //设置求解器
        SolverFactory<TaskAssignment> solverFactory = SolverFactory.createFromXmlInputStream(ins);
        Solver<TaskAssignment> solver = solverFactory.buildSolver();
        TaskAssignment unassignment = new TaskAssignment(machines, tasks);

        TaskAssignment assigned = solver.solve(unassignment);//启动引擎，获得求解

        List<Machine> machinesAssigned = assigned.getTaskList().stream().map(Task::getMachine).distinct().collect(Collectors.toList());

        //打印 求解结果
        for(Machine machine : machinesAssigned) {
            System.out.print("\n" + machine + ":");
            List<Task> tasksInMachine = assigned.getTaskList().stream().filter(x -> x.getMachine().equals(machine)).collect(Collectors.toList());
            for(Task task : tasksInMachine) {
                System.out.print("->" + task);
            }
        }
    }


    private static List<Machine> getMachines() {
        // 设置六个机台
        Machine m1 = new Machine(1, "Type_A", 300, 100);
        Machine m2 = new Machine(2, "Type_A", 1000, 100);
        Machine m3 = new Machine(3, "TYPE_B", 1000, 300);
        Machine m4 = new Machine(4, "TYPE_B", 1000, 100);
        Machine m5 = new Machine(5, "Type_C", 1100, 100);
        //Machine m6 = new Machine(6, "Type_D", 900, 100);

        List<Machine> machines = new ArrayList<Machine>();
        machines.add(m1);
        machines.add(m2);
        machines.add(m3);
        machines.add(m4);
        machines.add(m5);
        //machines.add(m6);

        return machines;
    }

    private static List<Task> getTasks(){
        // 设置10个任务
        Task t1 = new Task(1, "Type_A", 100);
        Task t2 = new Task(2, "Type_A", 100);
        Task t3 = new Task(3, "Type_A", 100);
        Task t4 = new Task(4, "Type_A", 100);
        Task t5 = new Task(5, "TYPE_B", 800);
        Task t6 = new Task(6, "TYPE_B", 500);
        Task t7 = new Task(7, "Type_C", 800);
        Task t8 = new Task(8, "Type_C", 300);
        Task t9 = new Task(9, "Type_D", 400);
        Task t10 = new Task(10, "Type_D", 500);

        List<Task> tasks = new ArrayList<Task>();
        tasks.add(t1);
        tasks.add(t2);
        tasks.add(t3);
        tasks.add(t4);
        tasks.add(t5);
        tasks.add(t6);
        tasks.add(t7);
        tasks.add(t8);
        tasks.add(t9);
        tasks.add(t10);

        return tasks;
    }
}